package com.homwee.serialportinspection;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.homwee.factory.R;

import com.homwee.serialportinspection.tv.Tv;

public class SerialPortInspectionActivity extends Activity {
    private static final String TAG = "SerialPortInspectionAct";

    private static final String SERVICE_NAME = "com.homwee.serialportinspection.InspectionService";

    private static SerialPortInspectionActivity SPIACTIVITY = null;

    private SerialReceiver receiver;
    private LinearLayout layout;
    private TextView ctValue;

    private Button startServiceButton;
    private Button stopServiceButton;

    /**
     * false:unregister
     * true:register
     */
    private boolean actionShowReceiverflag = false;

    public static SerialPortInspectionActivity getInstance() {
        return SPIACTIVITY;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.serialactivitylayout);

        SPIACTIVITY = this;

        //String buildTime = BuildConfig.BUILD_TIME;
        //Log.d(TAG, "[SerialPortInspection]BuildTime:" + buildTime);
        final Intent intentInpsectionService = new Intent(getApplicationContext(), InspectionService.class);
        intentInpsectionService.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startServiceButton = (Button) findViewById(R.id.button_startservices);
        stopServiceButton = (Button) findViewById(R.id.button_stopservices);

        layout = (LinearLayout) findViewById(R.id.serialactivity_layout);
        ctValue = (TextView) findViewById(R.id.ct_value);

        Bundle bundle = this.getIntent().getExtras();
        if (bundle != null) {
            if (bundle.getString("param").equals("rgb")) {
                rgbView();
            }
        }

        if (false == actionShowReceiverflag) {
            receiver = new SerialReceiver();
            IntentFilter inf = new IntentFilter(Tv.ACTION_SHOW);
            registerReceiver(receiver, inf);
            actionShowReceiverflag = true;
        }

        startServiceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Start services

                if (!InspectionService.isRunService(getApplicationContext(), SERVICE_NAME)) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        getApplicationContext().startForegroundService(intentInpsectionService);
                    } else {
                        getApplicationContext().startService(intentInpsectionService);
                    }

                    Toast.makeText(
                    SerialPortInspectionActivity.this, "Open Uart", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(
                        SerialPortInspectionActivity.this, "Uart is already opened", Toast.LENGTH_SHORT).show();
                }
            }
        });

        startServiceButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    startServiceButton.setBackgroundColor(Color.YELLOW);
                } else {
                    startServiceButton.setBackgroundColor(Color.GRAY);
                }
            }
        });

        stopServiceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (InspectionService.isRunService(getApplicationContext(), SERVICE_NAME)) {
                    getApplicationContext().stopService(intentInpsectionService);
                    Toast.makeText(
                SerialPortInspectionActivity.this, "Close Uart", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(
                SerialPortInspectionActivity.this, "Uart is closed", Toast.LENGTH_SHORT).show();
                }
            }
        });

        stopServiceButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    stopServiceButton.setBackgroundColor(Color.YELLOW);
                } else {
                    stopServiceButton.setBackgroundColor(Color.GRAY);
                }
            }
        });
    }

    class SerialReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            int content = intent.getIntExtra("content", 0);
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Intent intent = new Intent(Tv.ACTION_SHOW);
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                //return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (true == actionShowReceiverflag) {
            unregisterReceiver(receiver);
            actionShowReceiverflag = false;
        }
    }

    private void rgbView() {
        startServiceButton.setVisibility(View.GONE);
        stopServiceButton.setVisibility(View.GONE);
        layout.setBackgroundColor(Color.parseColor("#00000000"));
        ctValue.setVisibility(View.VISIBLE);
        int red = Settings.Global.getInt(getContentResolver(), "picture_red_gain", 0);
        int green = Settings.Global.getInt(getContentResolver(), "picture_green_gain", 0);
        int blue = Settings.Global.getInt(getContentResolver(), "picture_blue_gain", 0);
        ctValue.setText("R:" + red + "\n" + "G:" + green + "\n" + "B:" + blue);
    }
}

